package com.ht.demo.service.mapper.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.demo.common.constans.SystemConstant;
import com.ht.demo.dao.dao.system.SysUserDao;
import com.ht.demo.dao.domain.system.SysUserDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author hdengxian
 * @Date 2023/3/27 17:32
 */
@Component
public class SysUserMapper extends ServiceImpl<SysUserDao, SysUserDO> {
    @Resource
    private SysUserDao sysUserDao;

    public Integer insertSysUser(SysUserDO record) {
        record.setCreateTime(new Date());
        record.setDelFlag(SystemConstant.STR_0);
        record.setUpdateTime(new Date());
        return sysUserDao.insert(record);
    }

    public Integer updateSysUserById(SysUserDO record) {
        record.setUpdateTime(new Date());
        return sysUserDao.updateById(record);
    }

    public SysUserDO findUserByLoginNo(String loginNo) {
        LambdaQueryWrapper<SysUserDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserDO::getLoginNo, loginNo);
        return sysUserDao.selectOne(wrapper);
    }

    public Integer countByCondition(SysUserDO record) {
        LambdaQueryWrapper<SysUserDO> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StringUtils.isNotBlank(record.getLoginNo()), SysUserDO::getLoginNo, record.getLoginNo());
        wrapper.eq(StringUtils.isNotBlank(record.getUserName()), SysUserDO::getUserName, record.getUserName());
        wrapper.eq(record.getId() != null, SysUserDO::getId, record.getId());
        wrapper.eq(record.getSysDeptId() != null, SysUserDO::getSysDeptId, record.getSysDeptId());
        Integer count = sysUserDao.selectCount(wrapper);
        if (count == null) count = 0;
        return count;
    }


}
